package sweet.helper.cli

trait OptionsParser {
  
  /** Parse a argument array to pick up options flags. The left over argument are stored and 
   * return as new arg list. Options can be prefix with either "-" or "--". An option is a single
   * string line without space! You may specify parameter to option with an "=" sign. */
  def parseOpts(argv: Array[String]): (List[String], scala.collection.Map[String, String]) = {
    val opts = new scala.collection.mutable.HashMap[String, String]
    val args = new scala.collection.mutable.ListBuffer[String]
    
    def addArgOpt(arg: String) = { 
      val pos = arg.indexOf("=")
      if(pos <0) opts += (arg -> "true")
      else opts += (arg.substring(0, pos) -> arg.substring(pos+1))
    }
    
    for(arg <- argv){
      if(arg.startsWith("--")) addArgOpt(arg.substring(2))
      else if(arg.startsWith("-")) addArgOpt(arg.substring(1))
      else args += arg
    }
    (args.readOnly.toList, opts.readOnly)
  }
}
